﻿using DBModels;
using Method.DateTime;
using Method.Enum;
using Models;
using Models.VModels;
using RedisHelper.Article;
using RedisHelper.Search;
using RedisHelper.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Methods
{
    public class SearchMet
    {
        /// <summary>
        /// 实时搜索
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<string> RealLikeText(string text)
        {
            List<string> list = new List<string>(11);
            list.Add(text);
            var dataList = new Searcing().GetSearchList();
            dataList = dataList
                .Where(u => u.Name.ToString().IndexOf(text) >= 0)
                .OrderByDescending(u => Convert.ToInt32(u.Value))
                .ToList();
            for (int i = 0; i < (dataList.Count > 10 ? 10 : dataList.Count); i++)
            {
                list.Add(dataList[i].Name);
            }
            return list;
        }

        /// <summary>
        /// 添加关键词搜索次数
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public bool InsertHeat(string text)
        {
            return new Searcing().InsertSearch(text);
        }

        /// <summary>
        /// 获取所有匹配值
        /// </summary>
        /// <returns>一次10条</returns>
        public List<V_Search> FindResult(string uid, int userC, int artC, string text)
        {
            List<V_Search> retList = new List<V_Search>(10);
            UserStatistics us = new UserStatistics();

            using (ArticeContext context = new ArticeContext())
            {
                //先查用户
                int userCount = 0;
                var userList = context.UserInfos.ToList().Where(u => u.NickName.ToLower().IndexOf(text) >= 0 || u.Id.ToString().IndexOf(text) >= 0).ToList();
                //删除自己
                if (uid != null)
                    userList = userList.Where(u => u.Id != Convert.ToInt32(uid)).ToList();
                //判断是否够查
                if (userList.Count > userC)
                {
                    for (int i = 0; i < (userList.Count - userC > 5 ? 5 : userList.Count - userC); i++)
                    {
                        V_Search a = new V_Search()
                        {
                            IsArtOrUser = 1,
                            Outcome = new V_SearchUser()
                            {
                                UserId = userList[i + userC].Id,
                                NickName = userList[i + userC].NickName,
                                Avatar = StaticConfigPath.AvatarPath(userList[i + userC].Avatar),
                                Level = context.Accounts.FirstOrDefault(u => u.Id == userList[i + userC].Id).Level,
                                IsPay = uid is null ? false : us.GetMutualPay(Convert.ToInt32(uid), userList[i + userC].Id),
                                PayCount = us.GetPayCount(userList[i + userC].Id),
                                _PayCount = us.Get_PayCount(userList[i + userC].Id)
                            }
                        };
                        retList.Add(a);
                        userCount++;
                    }
                }
                //查文章标题
                int articelCount = 10 - userCount;
                Like like = new Like();
                var articleList = context.ArticleLists.Where(u => u.Title.ToLower().IndexOf(text) >= 0).Where(u => u.IsAllow == false).OrderByDescending(u=>u.Time).ToList();
                //是否够查
                if (articleList.Count > artC)
                {
                    for (int i = 0; i < (articleList.Count - artC > articelCount ? articelCount : articleList.Count - artC); i++)
                    {
                        V_Search a = new V_Search()
                        {
                            IsArtOrUser = 0,
                            Outcome = new V_SearchArticle()
                            {
                                ArticleId = articleList[i + artC].Id,
                                UserName = context.UserInfos.FirstOrDefault(u => u.Id == articleList[i + artC].ReleaseId).NickName,
                                Time = GetPastTime.getTime(articleList[i + artC].Time),
                                Title = articleList[i + artC].Title,
                                Context = articleList[i + artC].Text.Substring(0, articleList[i + artC].Text.Length < 100 ? articleList[i + artC].Text.Length : 100),
                                Classification = GetEnumValueByIndex.GetName<ClassificationEnum.classification>(articleList[i + artC].Classification),
                                LikeCount = like.GetArticleLikeCount(articleList[i + artC].Id),
                                CommentCount = new Comment().GetArticleCommentCount(articleList[i + artC].Id),
                                IsLike = uid is null ? false : like.IsArticleLike(articleList[i + artC].Id, Convert.ToInt32(uid))
                            }
                        };
                        retList.Add(a);
                    }
                }
                return retList;
            }
        }
    }
}
